//package com.fundodoo.sfe.admin.controller;
//
//import com.tpv.vc.common.annotation.Log;
//import com.tpv.vc.common.controller.BaseController;
//import com.tpv.vc.common.domain.PermissionInfo;
//import com.tpv.vc.common.domain.QueryRequest;
//import com.tpv.vc.common.domain.ResponseBo;
//import com.tpv.vc.common.util.IPUtils;
//import com.tpv.vc.system.domain.Dept;
//import com.tpv.vc.system.domain.User;
//import com.tpv.vc.system.domain.UserOnline;
//import com.tpv.vc.system.service.DeptService;
//import com.tpv.vc.system.service.SessionService;
//import com.tpv.vc.system.service.StateService;
//import com.tpv.vc.system.service.UserService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller;
//import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import java.io.File;
//import java.lang.management.ManagementFactory;
//import com.sun.management.OperatingSystemMXBean;
//
//import java.math.BigDecimal;
//import java.net.Inet4Address;
//import java.net.InetAddress;
//import java.net.NetworkInterface;
//import java.text.DecimalFormat;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * 状态监控
// *
// * @Author: denis.zheng
// * @Date: 2018/12/20 16:40
// * @Version 1.0
// */
//@Controller
//public class StateController extends BaseController {
//
//    @Autowired
//    SessionService sessionService;
//    @Autowired
//    StateService stateService;
//    private Logger log = LoggerFactory.getLogger(this.getClass());
//    @Autowired
//    private UserService userService;
//    
//    @Autowired
//    private DeptService deptService;
//
//    @Log("访问状态监控页面")
//    @RequestMapping("state")
//    public String index(Model model) {
////        User user = super.getCurrentUser();
////
////        model.addAttribute("user", user);
//
//
//        return "system/monitor/state";
//    }
//
//    @RequestMapping("state/meetingRoomCount")
//    @ResponseBody
//    public ResponseBo meetingRoomCount() {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Map<String, Integer> resultMap = this.stateService.getMeetingRoomCount(shiroDeptInfo.getDeptId());
//        log.info("totol:{},using:{}", resultMap.get("total"), resultMap.get("usingCount"));
////        Map<String,Object> returnMap=new HashMap<>();
////        returnMap.put("total",resultMap.get("total"));
////        returnMap.put("usingCount",resultMap.get("usingCount"));
//
//        return ResponseBo.ok(resultMap);
//    }
//
//    @RequestMapping("state/usersCount")
//    @ResponseBody
//    public ResponseBo usersCount(QueryRequest request, User user) {
//        PermissionInfo permissionInfo = super.getCurrentUserPermissionInfo();
//        User currUser=super.getCurrentUser();
//        //jinhui.li
//    	Dept dept = new Dept();
//		dept.setParentId(permissionInfo.getDeptId());
//    	dept.setSelf("0");
//		List<Dept> deptIds = this.deptService.findAllDepts(dept);
//		user.setDeptIds(deptIds.stream().map(Dept::getDeptId).collect(Collectors.toList()));
//
//        List<User> userList = this.userService.findUserWithDept(user, request);
//        List<User> userTempList = new ArrayList<User>(userList.size());
//        userTempList.add(currUser);
//        for (int i = 0; i < userList.size(); i++) {
//            User userTemp = userList.get(i);
//            //添加子级用户
//            if(!userTemp.getDeptId().equals(currUser.getDeptId())) {
//                userTempList.add(userTemp);
//            }
//        }
//        List<UserOnline> list = sessionService.list();
//        List<UserOnline> resultList = new ArrayList<UserOnline>();
//        if(list!=null) {
//            for (UserOnline userOnline : list) {
//                String userId = userOnline.getUserId();
//                for (User user_ : userTempList) {
//                    if (user_.getUserId().toString().equals(userId)) {
//                        resultList.add(userOnline);
//                    }
//                }
//            }
//        }
//        Map<String, Integer> resultMap = this.stateService.getGuestCount(permissionInfo.getDeptId()==1?null:permissionInfo.getDeptId());
//        Object object = resultMap.get("guestCount");
//        int guestCount=Integer.parseInt(String.valueOf(object));
//        Map<String, Object> userMap = new HashMap<>();
//        userMap.put("userCount", userList.size()+1+guestCount); //jinhui.li 和Steven 确认为其可查询组用户数据+1（自己）
//        userMap.put("userOnline", resultList.size());
//        userMap.put("guestCount",guestCount);
//        return ResponseBo.ok(userMap);
//    }
//
//    @RequestMapping("state/meetingCount")
//    @ResponseBody
//    public ResponseBo meetingCount() {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Map<String, Integer> resultMap = this.stateService.getMeetingCount(shiroDeptInfo.getDeptId()==1?null:shiroDeptInfo.getDeptId());
//        return ResponseBo.ok(resultMap);
//    }
//
//    @RequestMapping("state/attendeeCount")
//    @ResponseBody
//    public ResponseBo attendeeCount() {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Map<String, Integer> resultMap = this.stateService.getAttendeeCount(shiroDeptInfo.getDeptId()==1?null:shiroDeptInfo.getDeptId());
//        return ResponseBo.ok(resultMap);
//    }
//
//
//    @RequestMapping("state/durationCount")
//    @ResponseBody
//    public ResponseBo durationCount() {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Map<String, Integer> resultMap = this.stateService.getDurationCount(shiroDeptInfo.getDeptId()==1?null:shiroDeptInfo.getDeptId());
//        return ResponseBo.ok(resultMap);
//    }
//
//    @RequestMapping("state/userState")
//    @ResponseBody
//    public ResponseBo userState(QueryRequest request, User user) {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Dept dept = new Dept();
//        dept.setParentId(shiroDeptInfo.getDeptId());
//        dept.setSelf("0");
//        List<Dept> deptIds = this.deptService.findAllDepts(dept);
//        user.setDeptIds(deptIds.stream().map(Dept::getDeptId).collect(Collectors.toList()));
//        List<User> userList = this.userService.findUserWithDept(user, request);
//        Map<String, Integer> resultMap = this.stateService.getGuestCount(shiroDeptInfo.getDeptId()==1?null:shiroDeptInfo.getDeptId());
//        Map<String, Object> userMap = new HashMap<>();
//        Object object = resultMap.get("guestCount");
//        int guestCount=Integer.parseInt(String.valueOf(object));
//        int userCount=userList.size()+1;
//        userMap.put("userCount", userCount);
//        userMap.put("guestCount", guestCount);
//        userMap.put("total",guestCount+userCount);
//        return ResponseBo.ok(userMap);
//    }
//
//    @RequestMapping("state/meetingStats")
//    @ResponseBody
//    public ResponseBo meetingStats(String activeTab,String unit,String timeField) {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deptId",shiroDeptInfo.getDeptId()==1?null:shiroDeptInfo.getDeptId());
//        //unit 1.today 2.this week 3.this month 4.year
//        paramMap.put("unit",unit);
//        if("meetingNum".equals(activeTab)) {
//            //meeting count
//            paramMap.put("type", 1);
//        }else if("attendeeNum".equals(activeTab)){
//            // attendee num
//            paramMap.put("type", 2);
//        }else if("durationNum".equals(activeTab)){
//            // duration count
//            paramMap.put("type", 3);
//        }
//        List<Map<String,Integer>> numList=this.stateService.getMeetingNum(paramMap);
//        List<Map<String,Object>> meetingRootmList=this.stateService.getMeetingRoomRanking(paramMap);
//        Map<String, Object> resultMap = new HashMap<>();
//        resultMap.put("numList",numList);
//        resultMap.put("meetingRootmList",meetingRootmList);
//        return ResponseBo.ok(resultMap);
//    }
//
//    @RequestMapping("state/statsByGroup")
//    @ResponseBody
//    public ResponseBo statsByGroup(String activeTab) {
//        PermissionInfo shiroDeptInfo = super.getCurrentUserPermissionInfo();
//        Map<String, Object> paramMap = new HashMap<>();
//        paramMap.put("deptId",shiroDeptInfo.getDeptId()==1?null:shiroDeptInfo.getDeptId());
//        //type 1.meetingroom count by group 2.meeting count by group 3.meeting duration by group
//        if("userGroup-meetingRoomNum".equals(activeTab)) {
//            paramMap.put("type", 1);
//        }else if("userGroup-meetingNum".equals(activeTab)){
//            paramMap.put("type", 2);
//        }else if("userGroup-durationNum".equals(activeTab)){
//            paramMap.put("type", 3);
//        }
//        List<Map<String,Object>> statsByGroupList=this.stateService.getCountByGroup(paramMap);
//        return ResponseBo.ok(statsByGroupList);
//    }
//
//    @RequestMapping("state/systemInfo")
//    @ResponseBody
//    public ResponseBo systemInfo() {
//        try {
//            Map<String, Object> result = new HashMap<>();
////        File[] disks=File.listRoots();
////        for (File file:disks) {
////            result.put("freeSpace",new DecimalFormat("0.00").format((float)file.getFreeSpace()/1024/1024/1024)+"G");
////            result.put("usableSpace",new DecimalFormat("0.00").format((float)file.getUsableSpace()/1024/1024/1024)+"G");
////            result.put("totalSpace",new DecimalFormat("0.00").format((float)file.getTotalSpace()/1024/1024/1024)+"G");
////        }
//            OperatingSystemMXBean mem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
//            result.put("totalPhysicalMemorySize", (Math.round((float) mem.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024)));
//            result.put("freePhysicalMemorySize", (new DecimalFormat("0.00").format((float) mem.getFreePhysicalMemorySize() / 1024 / 1024 / 1024)));
//            result.put("usagePhysicalMemorySize",new DecimalFormat("0.00").format((float) (mem.getTotalPhysicalMemorySize()-mem.getFreePhysicalMemorySize()) / 1024 / 1024 / 1024));
////        result.put("committedVirtualMemorySize",(mem.getCommittedVirtualMemorySize()/1024/1024/1024)+"G");
////        result.put("freeSwapSpaceSize",(mem.getFreeSwapSpaceSize()/1024/1024/1024)+"G");
////        result.put("totalSwapSpaceSize",(mem.getTotalSwapSpaceSize()/1024/1024/1024)+"G");
////        result.put("processCpuLoad",mem.getProcessCpuLoad());
////        result.put("systemCpuLoad",mem.getSystemCpuLoad());
////        result.put("processCpuTime",mem.getProcessCpuTime());
//            result.put("availableProcessors", mem.getAvailableProcessors());
////            InetAddress addr = InetAddress.getLocalHost();
////            String ip = addr.getHostAddress();
////            String address = addr.getHostName();
////            result.put("hostAddress",ip);
////            result.put("hostName",address);
//            Map<String,String> ipAndCity=stateService.getServerIpAndCity();
//            result.put("hostAddress", ipAndCity.get("ip")==null?IPUtils.getRealIp():ipAndCity.get("ip"));
//            result.put("city", ipAndCity.get("city")==null?"Unknown":ipAndCity.get("city"));
//            return ResponseBo.ok(result);
//        }catch (Exception e){
//            return ResponseBo.error(e.getMessage());
//        }
//    }
//}
